Passed
Push — master ( 908e5f...70f013 )
by Dongxin
01:32
created

$(document).ready   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
'use strict';
2
3
function decryptAES (password) {
4
5
  try {
6
7
    var decryptionError = String(document.getElementById('decryptionError').innerHTML);
8
    var noContentError = String(document.getElementById('noContentError').innerHTML);
9
10
  } catch (e) {
11
12
    decryptionError = 'Incorrect Password!';
13
    noContentError = 'No content to display!';
14
15
  }
16
17
  try {
18
19
    let content = CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), password);
20
    content = content.toString(CryptoJS.enc.Utf8);
21
    content = decodeBase64(content);
22
    content = unescape(content);
23
    if (content === '') {
24
25
      throw new Error(noContentError); // ???
26
27
    } else {
28
29
      document.getElementById('encrypt-blog').style.display = 'inline';
30
      document.getElementById('encrypt-blog').innerHTML = '';
31
32
      // Use jquery to load some js code
33
      try {
34
35
        $('#encrypt-blog').html(content);
36
37
        // NO Style Change here
38
        {callback}
39
        // NO Style Change here
40
41
      } catch(e) {
42
43
        const errorInfo = '<p>'
44
                      + 'Some errors occurred, check the original file please.'
45
                      + 'Detailed exceptions are shown in console.'
46
                      + '</p>';
47
        console.error(e);
48
        $('#encrypt-blog').html(errorInfo);
49
50
      }
51
52
      document.getElementById('hbe-security').style.display = 'none';
53
      if (document.getElementById('toc-div')) {
54
55
        document.getElementById('toc-div').style.display = 'inline';
56
57
      }
58
59
    }
60
61
    // Call MathJax to render
62
    if(typeof MathJax !== 'undefined') {
63
64
      try {
65
66
        MathJax.Hub.Queue(
67
          ['resetEquationNumbers', MathJax.InputJax.TeX, ],
68
          ['PreProcess', MathJax.Hub, ],
69
          ['Reprocess', MathJax.Hub, ]
70
        );
71
72
      } catch (e) {
73
74
        console.log('Can\'t render with MathJax');
75
76
      }
77
78
    }
79
80
  } catch (e) {
81
82
    alert(decryptionError);
83
    console.log(e);
84
    return false;
85
86
  }
87
88
  return true;
89
90
}
91
92
function htmlDecode (str) {
93
94
  let s = '';
95
  if (str.length == 0) {
96
97
    return '';
98
99
  }
100
101
  s = str.replace(/&gt;/g, '&');
102
  s = s.replace(/&lt;/g, '<');
103
  s = s.replace(/&gt;/g, '>');
104
  s = s.replace(/&nbsp;/g, '    '); // ??? why not ' '
105
  s = s.replace(/'/g, '\'');
106
  s = s.replace(/&quot;/g, '"');
107
  s = s.replace(/<br>/g, '\n');
108
  return s;
109
110
}
111
112
function decodeBase64 (content) {
113
114
  content = CryptoJS.enc.Base64.parse(content);
115
  content = CryptoJS.enc.Utf8.stringify(content);
116
  return content;
117
118
}
119
120
function setCookie (cookieName, cookieValue, expireMinutes) {
121
122
  const expireTime = new Date(new Date().getTime() + 1000 * 60 * expireMinutes);
123
  document.cookie = `${ cookieName }=${ escape(cookieValue) }${ expireMinutes == null ? '' : `;expires=${ expireTime.toGMTString() }` }`;
124
125
}
126
127
function getCookie (cookieName) {
128
129
  if (document.cookie.length > 0) {
130
131
    let idx = document.cookie.indexOf(`${ cookieName }=`);
132
    if (idx != -1) {
133
134
      idx = idx + cookieName.length + 1;
135
      let idy = document.cookie.indexOf(';', idx);
136
      if (idy == -1) {
137
138
        idy = document.cookie.length;
139
140
      }
141
      return unescape(document.cookie.substring(idx, idy));
142
143
    }
144
145
  }
146
  return '';
147
148
}
149
150
// Since you decided to use jQuery.
151
$(document).ready(
152
  function () {
153
154
    const COOKIE_NAME = 'HBE-PASSWORD';
155
156
    let password = String(getCookie(COOKIE_NAME));
157
    console.log('Get password from Cookie:' + password);
158
159
    if (password != '') {
160
161
      if(!decryptAES(password)) {
162
163
        // Delete cookie
164
        setCookie(COOKIE_NAME, password, -5);
165
166
      }
167
168
    }
169
170
    console.log('Registering Enter for decrypt.');
171
    document.getElementById('pass').onkeypress = function (keyPressEvent) {
172
173
      password = String(document.getElementById('pass').value);
174
      if (keyPressEvent.keyCode === 13) {
175
176
        const result = decryptAES(password);
177
178
        if (result) {
179
180
          setCookie(COOKIE_NAME, password, 30);
181
182
        }
183
184
      }
185
186
    };
187
188
  }
189
);
190